package com.ym.wcp.framework.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Constructor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    public static String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    public static <T> IPage<T> getIPageByPageInfo(PageInfo<T> info) {
        Page<T> p = new Page<>();
        if (info != null) {
            p.setTotal(info.getTotal());
            p.setSize(info.getPageSize());
            p.setCurrent(info.getPageNum());
            p.setPages(info.getPages());
            p.setRecords(info.getList());
        }
        return p;
    }

    public static <T> PageInfo<T> getPageInfo(Long count, Integer page, Integer size) {
        page += 1;
        PageInfo<T> info = new PageInfo<>();
        info.setPrePage(page);
        info.setPageNum(page);
        info.setSize(size);
        info.setPageSize(size);
        if (count != null) {
            long pageCount = count % size == 0 ? count / size : (count / size) + 1;
            info.setTotal(count);
            info.setPages((int) pageCount);
            info.setIsLastPage(page == pageCount);
        }
        return info;
    }

    public static String getRequestHeader(String name) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request != null) {
            return request.getHeader(name);
        }
        return null;
    }

    public static <T> T getDefaultAttribute(Class<T> clazz, String header) {
        if (clazz != null) {
            try {
                Constructor c = clazz.getConstructor(String.class);
                String h = getRequestHeader(header);
                return (T) c.newInstance(h);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //驼峰转下划线
    public static String camelToUnderline(String param) {
        if (!StringUtils.isEmpty(param)) {
            int len = param.length();
            StringBuilder sb = new StringBuilder(len);
            for (int i = 0; i < len; i++) {
                char c = param.charAt(i);
                if (Character.isUpperCase(c)) {
                    sb.append("_");
                }
                sb.append(Character.toLowerCase(c));  //统一都转小写
            }
            return sb.toString();
        }
        return null;
    }

    //下划线转驼峰
    public static String underlineToHump (String str){
        //正则匹配下划线及后一个字符，删除下划线并将匹配的字符转成大写
        Pattern UNDERLINE_PATTERN = Pattern.compile("_([a-z])");
        Matcher matcher = UNDERLINE_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer(str);
        if (matcher.find()) {
            sb = new StringBuffer();
            //将当前匹配的子串替换成指定字符串，并且将替换后的子串及之前到上次匹配的子串之后的字符串添加到StringBuffer对象中
            //正则之前的字符和被替换的字符
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
            //把之后的字符串也添加到StringBuffer对象中
            matcher.appendTail(sb);
        } else {
            //去除除字母之外的前面带的下划线
            return sb.toString().replaceAll("_", "");
        }
        return underlineToHump(sb.toString());
    }
}
